<?php

namespace Frieza\Modules\Position\Controllers;

use Doraemon\pockets\constInterface\LinkConf;
use Doraemon\service\position\AdmExtOperationModule;
use Doraemon\tools\BlockTypeMapping;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;


class SearchResultCardController extends BaseController
{
    use BaseTrait;

    const ALLOW_LINK = [
        LinkConf::LINK_PHOTO,
        LinkConf::LINK_ARTICLE,
        LinkConf::LINK_BLANK,
        LinkConf::LINK_GUIDE,
    ];

    public function extendDefaultBaseInfo()
    {
        return [
            'type' => 1,
            'key_word' => $this->relation['ad_unique'] ?? ''
        ];
    }

    public function beforeTitleForm(Form $form)
    {
        $form->number('base.pos', '展示位置')->max(20)->required();
        $form->text('key_word', '关键词')->required()->help("多个关键词使用,隔开;例如:厨房,客厅");
        $form->radio('type','搜索结果列表形式')
            ->options([1 => '内容', 2 => '图片'])
            ->when(1, function (Form $form) {
                $form->multiLinkSelect('base.link', '内容', self::ALLOW_LINK)->help('支持整屋案例');
            })->when(2, function (Form $form) {
                $this->bannerForm2SearchResultCard($form);
                $form->multiLinkSelect('base.link', '图片链接');
            });
    }

    public function saveModule(Request $request, $baseInfoId)
    {
        $type = $request->input('type', '1');
        $key_word = $request->input('key_word', '');
        $obj = $request->input("base.link", []);
        $pic_id = $request->input("base.pic_id", '');
        $single_pic_id = $request->input("base.single_pic_id", '');
        $link_type = $obj['link_type'] ?? '';
        $link_params = isset($obj['link_params']) ? json_encode($obj['link_params'], JSON_UNESCAPED_UNICODE) : '';
        $objRs = BlockTypeMapping::getObjFromLinkParams($link_type, $link_params);
        $material_id = $objRs['obj_id'] ?? null;
        $rules = $request->input('rules', []);
        $base = $request->input('base', []);
        $id = $base['id'] ?? 0;
        $copy_id = $base['copy_id'] ?? 0;
        if ($type == 1) {
            if (empty($material_id)) throw new \Exception('内容不能为空');

            $data = [
                'position_id' => $baseInfoId,
                'key_word' => $key_word,
                'material_id' => $material_id,
            ];
            AdmExtOperationModule::addKaBaseInfo($data);
        } else {
            if (empty($pic_id)) throw new \Exception('双列图片不能为空');

            if (empty($single_pic_id)) throw new \Exception('单列图片不能为空');

            $rule = array_map(function ($v) {
                return $v[0] ?? '';
            }, $rules);
            $img_size = $rule['img_size'] ?? 0;
            if (!(($id > 0 || $copy_id > 0) && $img_size == 0)) $this->rulesValid($rule, 0);

            $rule = array_map(function ($v) {
                return $v[1] ?? '';
            }, $rules);
            $img_size = $rule['img_size'] ?? 0;
            if (!(($id > 0 || $copy_id > 0) && $img_size == 0)) $this->rulesValid($rule, 1);

            $data = [
                'position_id' => $baseInfoId,
                'key_word' => $key_word,
            ];
            AdmExtOperationModule::addKaBaseInfo($data);
        }

    }

    public function extendList(Grid $grid)
    {
        $grid->column('pos', '展示位置')->display(function ($title, Grid\Column $column) {
            $pos = $this->pos % 4;
            $map = ['primary','warning','success','info','danger'];
            return $column->label($map[$pos]);
        });
        $grid->column('ext->key_word', '关键词');
        $grid->column('client_uid', '设计师账号uid');
    }
}
